package cn.jasonone.tio.tcp.handler.impl;

import cn.jasonone.tio.tcp.commons.Command;
import cn.jasonone.tio.tcp.commons.SystemCommand;
import cn.jasonone.tio.tcp.handler.CommandHandler;
import cn.jasonone.tio.tcp.handler.HeartbeatHandler;
import cn.jasonone.tio.tcp.model.Data;
import cn.jasonone.tio.tcp.model.Heartbeat;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MarkerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.tio.core.ChannelContext;

import javax.annotation.Resource;

/**
 * 心跳命令处理器
 *
 * @author xujie
 * @version 1.0
 * @date 2022/1/18 16:06
 */
@Slf4j
public class HeartbeatCommandHandler implements CommandHandler<Data> {
    @Autowired(required = false)
    private HeartbeatHandler heartbeatHandler;

    @Override
    public Command getCommand() {
        return SystemCommand.Heartbeat;
    }

    @Override
    public boolean isPacket() {
        return true;
    }

    @Override
    public void handler(Data data, ChannelContext ctx) throws Exception {
        log.debug(MarkerFactory.getMarker("Heartbeat"), "{}", data);
        if (heartbeatHandler != null) {
            heartbeatHandler.onHeartbeat(data.getBody(), ctx);
        }
    }
}
